package cn.sanch.org.service.impl;

import cn.sanch.basic.service.impl.BaseServiceImpl;
import cn.sanch.org.domain.Department;
import cn.sanch.org.mapper.DepartmentMapper;
import cn.sanch.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门服务实现类
 * @author sanch37
 * 2022/5/22
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    /**
     * 注入DepartmentMapper对象
     */
    @Autowired
    private DepartmentMapper departmentMapper;



    /**
     * 添加
     * @param department Department department
     */
    @Transactional
    @Override
    public void save(Department department) {

         //添加之后会返回一个自增长id值
         //添加的时候处理dirPath
         //创建一个空字符串的dirPath
         //表示添加的时候没有选择父部门 或者选择了之后在框内清除了选择
        departmentMapper.save(department);
        String dirPath = "";
        if (department.getParent_id() == null) {
            //那自己的dirPath就等于”/“拼接上自己的id
            dirPath = "/" + department.getId();
        //选择了父部门
        }else {
            //获取上级部门的id
            Long parent_id = department.getParent_id();
            //获取父部门的dirPath
            String parentDirPath = departmentMapper.findOne(parent_id).getDirPath();
            //那自己的dirPath就等于父部门的dirPath拼接”/“再拼接自己的id
            dirPath = parentDirPath + "/" + department.getId();
        }
        /**
         * 将更改后的dirPath设置到实体类对象中
         */
        department.setDirPath(dirPath);
        /**
         * 调用方法修改数据库中的数据
         */
        departmentMapper.update(department);
    }

    /**
     * 修改
     * @param department Department department
     */
    @Transactional
    @Override
    public void update(Department department) {

        /**
         * 修改的时候处理dirPath
         * 创建一个空字符串的dirPath
         */
        String dirPath = "";
        //表示添加的时候没有选择父部门 或者选择了之后在框内清除了选择
        if (department.getParent_id() == null) {
            //那自己的dirPath就等于”/“拼接上自己的id
            dirPath = "/" + department.getId();
        //选择了父部门
        }else {
            //获取上级部门的id
            Long parent_id = department.getParent_id();
            //获取父部门的dirPath
            String parentDirPath = departmentMapper.findOne(parent_id).getDirPath();
            //那自己的dirPath就等于父部门的dirPath拼接”/“再拼接自己的id
            dirPath = parentDirPath + "/" + department.getId();
        }
        //将更改后的dirPath设置到实体类对象中
        department.setDirPath(dirPath);
        //调用方法修改数据库中的数据
        departmentMapper.update(department);
    }




    /**
     * 获取部门树
     * @return
     */
    @Override
    public List<Department> deptTree() {
        /* 创建一个map集合，将department对象的id值和整个对象存入map中 */
        Map<Long, Department> map = new HashMap<>();
        /* 查询所有的对象 */
        List<Department> all = departmentMapper.findAll();

        /* 遍历 */
        for (Department department : all) {
            /* 存入map集合 */
            map.put(department.getId(), department);
        }

        /* 创建一个部门树list集合：装父级部门 */
        List<Department> deptTree = new ArrayList<Department>();
        for (Department department : all) {
            /* 判断对象是否有有父级id */
            if (department.getParent_id() == null) {
                /* 如果没有，就表示这个对象就是父级部门，存入部门树中 */
                deptTree.add(department);
            }else { //如果有父级id值，就存入父级id值的子集合children中
                /* 获取出这个对象的父级id */
                Long parent_id = department.getParent_id();
                /* 根据这个id找到父级部门从map集合中找 */
                Department department1 = map.get(parent_id);
                /* 将当前对象存入父级部门department1的children集合中 */
                department1.getChildren().add(department);
            }
        }
        /* 最后返回这个list集合deptTree */
        return deptTree;
    }
}
